<?php
/*
 *   Pmail easy way to send emails
 *   Copyright (C) 2009  Felipe Theodoro Gonçalves (www.ftgoncalves.com)
 *
 *   This program is free software: you can redistribute it and/or modify
 *	 it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class AdministratorsController extends PmailAppController {

	public $uses = array('Pmail.Administrator', 'Pmail.Config', 'Pmail.Group', 'Pmail.Report');

	public function index(){

		$this->set('page', 'home');

		// Verifica as configurações iniciais
		$this->Config->id = 1;
		$config = $this->Config->read();

		if($config['Config']['active'] == 1){

			$this->set('init_config', true);

		}else{

			$this->set('init_config', false);

		}

		// Verifica os grupos
		$groups = $this->Group->find('count');

		if($groups > 0){

			$this->set('groups', true);

		}else{

			$this->set('groups', false);

		}

		// Reports
		$report = array();
		$report = $this->Report->find('first', array(
			'order' => 'Report.id DESC'
		));

		$this->set('report', $report);
		$this->render();

	}

	/**
	 * Mostra o ip do usuário anterior
	 *
	 */
	public function beforeFilter(){

		// ip
		$this->Administrator->id = 1;
		$admin = $this->Administrator->read();
		$this->set('ip', $admin['Administrator']['ip']);

	}

	/**
	 * Seta o ip do usuário que entro agora
	 *
	 */
	public function afterFilter(){

		$this->Administrator->id = 1;
		$this->Administrator->saveField('ip', $_SERVER['REMOTE_ADDR']);

	}

	/**
	 * Set language
	 * @param $lang = eng
	 */
	public function set_language($lang = 'eng'){

		$this->Session->write('Language', $lang);

		$this->redirect('/pmail');
	}
}